package com.xul.srb.core.controller.api;


import com.baomidou.mybatisplus.extension.api.R;
import com.xul.common.exception.Assert;
import com.xul.common.result.ResponseEnum;
import com.xul.common.result.Result;
import com.xul.srb.base.uitl.JwtUtils;
import com.xul.srb.core.pojo.vo.LoginVO;
import com.xul.srb.core.pojo.vo.RegisterVO;
import com.xul.srb.core.pojo.vo.UserIndexVO;
import com.xul.srb.core.pojo.vo.UserInfoVO;
import com.xul.srb.core.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户基本信息 前端控制器
 * </p>
 *
 * @author xul
 * @since 2021-11-06
 */
@Api(tags = "前台会员管理")
@RestController
// @CrossOrigin
@RequestMapping("/api/core/userInfo")
public class UserInfoController {

    @Resource
    private UserInfoService userInfoService;

    @ApiOperation("会员注册")
    @PostMapping("/register")
    public Result register(@RequestBody RegisterVO registerVO) {
        // 用户注册
        userInfoService.register(registerVO);
        return Result.ok().message("用户注册成功");
    }

    @ApiOperation("会员登录")
    @PostMapping("/login")
    public Result login(@RequestBody LoginVO loginVO, HttpServletRequest request) {
        // 会员登录
        UserInfoVO userInfoVO = userInfoService.login(loginVO, request);
        return Result.ok().message("用户登录成功").data("userInfo", userInfoVO);
    }

    @ApiOperation("校验令牌")
    @GetMapping("/checkToken")
    public Result checkToken(HttpServletRequest request) {
        String token = request.getHeader("token");
        // 判断token是否有效
        boolean result = JwtUtils.checkToken(token);
        Assert.isTrue(result, ResponseEnum.LOGIN_AUTH_ERROR);
        return Result.ok();
    }


    @ApiOperation("校验手机号是否注册")
    @GetMapping("/checkMobile/{mobile}")
    public Result checkMobile(@PathVariable String mobile){
        boolean isExit =  userInfoService.checkMobile(mobile);
        return Result.ok().data("isExit",isExit);
    }

    @ApiOperation("获取个人空间用户信息")
    @GetMapping("/auth/getIndexUserInfo")
    public Result getIndexUserInfo(HttpServletRequest request) {
        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);
        UserIndexVO userIndexVO = userInfoService.getIndexUserInfo(userId);
        return Result.ok().data("userIndexVO", userIndexVO);
    }
}

